﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace FrozenBubble
{
    public class CompressorSprite : IScreenComponent
    {
        private GameScreen _screen;

        private IPlayerDimensions _dimensions;
        private int _newRootLevel;
        private Texture2D _compressor;
        private Texture2D _extension;

        private bool contentLoaded;

        private float x;
        private float y;

        private Vector2 _position;
        private List<Vector2> _extensionPositions;

        public CompressorSprite(GameScreen screen, IPlayerDimensions dimensions, int compressorRootLevel) 
        {
            _extensionPositions = new List<Vector2>();
            _screen = screen;
            _dimensions = dimensions;
            _newRootLevel = compressorRootLevel;
        }

        public void LoadContent()
        {
#if DEBUG
            long start = DateTime.Now.Ticks;
#endif

            _compressor = _screen.Cache.CompressorMain;
            _extension = _screen.Cache.CompressorExtension;

            x = _dimensions.CompressorPositionX;
            y = (float)(_dimensions.InitialTopLimit + _newRootLevel * BubbleConstants.ROW_SIZE);

            _position.X = x - _compressor.Width / 2;
            _position.Y = y - _compressor.Height;

            CalculateExtensionPositions();

            contentLoaded = true;

#if DEBUG
            long loadTime = (DateTime.Now.Ticks - start) / 10000;
            Debug.WriteLine("Loadtime CompressorSprite.cs: " + loadTime);
#endif
        }

        public void Update(GameTime gameTime) { }
        public void UnloadContent() { }

        public void Lower()
        {
            _newRootLevel++;

            y = (float)(_dimensions.InitialTopLimit + _newRootLevel * BubbleConstants.ROW_SIZE);
            _position.Y = y - _compressor.Height;

            CalculateExtensionPositions();
        }

        public void Reset()
        {
            _newRootLevel = 0;

            if (contentLoaded)
            {
                y = (float)(_dimensions.InitialTopLimit + _newRootLevel * BubbleConstants.ROW_SIZE);
                _position.Y = y - _compressor.Height;

                CalculateExtensionPositions();
            }
        }

        public int NewRootLevel
        {
            get
            {
                return _newRootLevel;
            }
        }

        public Vector2 Position
        {
            get
            {
                return _position;
            }
        }

        public IEnumerable<Vector2> Extensions
        {
            get
            {
                return _extensionPositions;
            }
        }

        public Texture2D CompressorTexture
        {
            get
            {
                return _compressor;
            }
        }

        public Texture2D ExtensionTexture
        {
            get
            {
                return _extension;
            }
        }

        #region Private Methods

        private void CalculateExtensionPositions()
        {
            _extensionPositions.Clear();

            float height = y - _compressor.Height + 3;

            while (height > 0)
            {
                _extensionPositions.Add(new Vector2(x - _extension.Width / 2, height - _extension.Height));
                height -= _extension.Height;
            }
        }

        #endregion
    }
}
